/**
 *FontChooser.java
 *@anthor Daniel Liu
 *2006-5-9 ����03:05:11
 **/
package org.lex.swing.fontchooser;

import java.awt.Component;
import java.awt.Font;
import java.awt.Window;

import javax.swing.SwingUtilities;

import org.lex.awt.Location;
import org.lex.swing.Option;
import org.lex.swing.fontchooser.simple.SimpleFontChooserDialog;


public class JFontChooser {
	public static final String FONT_CHANGED_PROPERTY = "JFontChooser.fontChanged";
	private static final String DEFAULT_TITLE = "Font";
	private Font font = null;
	private String title = DEFAULT_TITLE;
	private SimpleFontChooserDialog dialog = null;

	public JFontChooser() {
		this(new Font(Font.SANS_SERIF, Font.BOLD | Font.ITALIC, 36));
	}

	public JFontChooser(Font font) {
		this.setFont(font);
	}

	/**
	 * 
	 * @param parent
	 * @param title
	 * @param font
	 * @return font the font selected, or null if cancelled
	 */
	public static Font showDialog(Component parent, String title, Font font) {
		JFontChooser fc = new JFontChooser(font);
		fc.setTitle(title);
		Window root = null;
		if (null != parent)
			root = SwingUtilities.getWindowAncestor(parent);
		Option opt = fc.showDialog(root);
		Font selected = null;
		if (opt.isYesOption())
			selected = fc.getFont();
		fc = null;
		return selected;
	}

	public Font getFont() {
		return font;
	}

	public void setFont(Font font) {
		this.font = font;
		if (null != this.dialog && null != font)
			dialog.setSelectedFont(font);
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Option showDialog(Window parent) {
		dialog = new SimpleFontChooserDialog(parent);
		dialog.setTitle(title);
		if (null != font)
			dialog.setSelectedFont(font);
		if (null != parent)
			Location.moveToParentCenter(parent, dialog);
		dialog.setVisible(true);
		Option opt = dialog.getSelectedOption();
		if (opt.isYesOption())
			font = dialog.getSelectedFont();
		dialog.dispose();
		dialog = null;
		return opt;
	}

	public static void main(String[] args) {
		new JFontChooser().showDialog(null);
	}
}
